package com.green.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.green.business.domain.Goods;
import com.green.business.domain.Provider;
import com.green.business.domain.Salesback;
import com.green.business.service.*;
import com.green.business.vo.InportVo;
import com.green.system.common.DataGridView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.green.business.mapper.InportMapper;
import com.green.business.domain.Inport;

/**
 * Created with IntelliJ IDEA.
 * @Author: Green
 * @Date: 2021/11/18/0:34
 * @Description: 
 */


@Service
public class InportServiceImpl extends ServiceImpl<InportMapper, Inport> implements InportService{

    @Autowired
    private InportMapper inportMapper ;

    @Autowired
    private ProviderService providerService;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private SalesService salesService;

    @Autowired
    private ScrapService scrapService ;

    @Autowired
    private OutportService outportService;

    @Override
    public DataGridView queryAllInport(InportVo inportVo) {

        IPage<Inport> page = new Page<>(inportVo.getPage(),inportVo.getLimit());
        QueryWrapper<Inport> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq(inportVo.getGoodsid()!=null,"goodsid",inportVo.getGoodsid());
        queryWrapper.eq(inportVo.getProviderid()!=null,"providerid",inportVo.getProviderid());

        queryWrapper.ge(inportVo.getStartTime()!=null,"inporttime",inportVo.getStartTime());
        queryWrapper.le(inportVo.getEndTime()!=null,"inporttime",inportVo.getEndTime());
        queryWrapper.orderByDesc("inporttime");

        this.inportMapper.selectPage(page,queryWrapper);
        List<Inport> records = page.getRecords();
        for (Inport record : records) {
            if (null!=record.getGoodsid()){
                Goods good = this.goodsService.getById(record.getGoodsid());
                record.setGoodsname(good.getGoodsname());
                record.setSize(good.getSize());
                record.setNumberNow(good.getNumber());
                record.setNumberSale(this.salesService.querySalesNumberById(record.getGoodsid()));
                record.setNumberScrap(this.scrapService.querySalesNumberById(record.getGoodsid()));
                record.setNumberBack(this.outportService.querySalesNumberById(record.getGoodsid()));
            }
            if (null!=record.getProviderid()){
                Provider provider = this.providerService.getById(record.getProviderid());
                record.setProvidername(provider.getProvidername());
            }
        }
        return new DataGridView(page.getTotal(),records);
    }

    @Override
    public Inport saveInport(Inport inport) {
        this.inportMapper.insert(inport);

//        //更新库存
//        Integer goodsId = inport.getGoodsid();
//        Goods goods = this.goodsService.getById(goodsId);
//        goods.setNumber(goods.getNumber()+inport.getNumber());
//        this.goodsService.updateGoods(goods);
        return inport;
    }

    @Override
    public Inport updateInport(Inport inport) {
        Inport oldObj = this.inportMapper.selectById(inport.getId());
        Goods goods = this.goodsService.getById(oldObj.getGoodsid());
        goods.setNumber(inport.getNumber()-inport.getNumberSale()-inport.getNumberBack()-inport.getNumberScrap());
        this.goodsService.updateGoods(goods);
        this.inportMapper.updateById(inport);
        return inport;
    }

    @Override
    public boolean removeById(Serializable id) {
//        Inport inport = this.inportMapper.selectById(id);
//        Goods goods = this.goodsService.getById(inport.getGoodsid());
//        goods.setNumber(goods.getNumber()-inport.getNumber());
//        this.goodsService.updateGoods(goods);
        return super.removeById(id);
    }
}
